<template>
  <view>
    <view class="goods-list">
      <view v-for="(item,i) in goodsList" :key="i" @click="gotoDetile(item)">
      <my-goods :item="item"></my-goods>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        queryObj:{
          query:'' ,//查询的关键字
          cid:'' ,//分类ID
          pagesize: 10 ,//每页长度
          pagenum: 1 ,//页数索引
        } ,
        goodsList:[], //商品数据
        total:0,
        isloading:false //节流阀
       
      };
    },
    onLoad(options){
      this.queryObj.query=options.query || ''
      this.queryObj.cid=options.cid ||''
      this.getGoodsList()
    },
    methods:{
      
      async getGoodsList(cb){
        this.isloading=true
          const {data:res} =await uni.$http.get('/api/public/v1/goods/search',this.queryObj)
          // console.log(res)
          this.isloading=false
          cb && cb()
          if(res.meta.status!==200) return uni.$showMsg()
          this.goodsList=[...this.goodsList,...res.message.goods]
          this.total=res.message.total
          
        },
        gotoDetile(item){
          uni.navigateTo({
            url:'/subpkg/goods_detile/goods_detile?goods_id='+item.goods_id
          })
        }
    },
    // 下拉加载
    onReachBottom() {
      // 判断数据是否加载完毕
      if(this.queryObj.pagenum*this.queryObj.pagesize>=this.total) return uni.$showMsg('数据加载完毕')
      
      if(this.isloading) return
      // 让页码值自增+1
      this.queryObj.pagenum++
      this.getGoodsList()
    },
    // 上拉刷新
    onPullDownRefresh(){
      // 重置关键数据
        this.queryObj.pagenum = 1
        this.goodsList = []
        this.total = 0
        this.isloading = false
        this.getGoodsList(()=>uni.stopPullDownRefresh())
    }

  }
</script>

<style lang="scss">

</style>
